package views;

import controllers.EventHandler;
import java.awt.Color;
import java.awt.Container;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.border.LineBorder;

public class ProgressBar extends JWindow implements Runnable {

    public static JProgressBar jpb;
    private final int WIDTH = 300;
    private final int HEIGHT = 150;
    private static Container c;
    private static JLabel generate;
    public static int progress = 0;
    private static Calendar cal;

    public ProgressBar(int x, int y, Calendar cal) {

        progress = 0;
        this.cal = cal;
        this.setBounds(x - WIDTH / 2, y - HEIGHT / 2, WIDTH, HEIGHT);
        c = this.getContentPane();
        this.setAlwaysOnTop(true);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBorder(new LineBorder(Color.BLACK));
        generate = new JLabel("Generating Schedule... Please wait");
        generate.setBounds(10, 10, 200, 30);
        jpb = new JProgressBar();
        jpb.setValue(progress);
        jpb.setStringPainted(true);
        jpb.setBounds(0, 50, WIDTH, 30);
        panel.add(generate);
        panel.add(jpb);
        c.add(panel);
        this.setVisible(true);
        Thread f = new Thread(this);
        f.start();

    }

    public static void setProgress(int p) {
        progress = p;
    }

    @Override
    public void run() {

        Thread t = new Thread(new createEvent(cal));
        t.start();
        
        while (progress < 100) {
            jpb.setValue(progress);
        }
        mainFrame.updateTable();
        this.dispose();
    }
}

class createEvent implements Runnable {

    private Calendar cal;
    private static EventHandler eventHandler = new EventHandler();

    public createEvent(Calendar cal) {
        this.cal = cal;
    }

    @Override
    public void run() {
        eventHandler.generateEvents(cal);
    }
}
